import turtle as t
import random
c = random.randint(-200,200)
d = random.randint(-200,200)
def s():
    t.up()
    t.goto(c,d)
    t.down()
    t.dot(20,'blue')


xy = [[0,0],[-20,0],[-40,0]]
a = 1
b = 20
def mowe1():
    global a, b,c ,d
    t.clear()
    s()
    for i in xy:
        t.up()
        t.goto(i)
        t.down()
        t.dot(20,'red')
    pos = xy[0].copy()

    if abs(pos[0] - c) < 20 and abs((pos[1] - d)) < 20:
        print(pos, c,d)
        c = random.randint(-200, 200)
        d = random.randint(-200, 200)
    else:
        del xy[-1]

    pos[a] += b
    xy.insert(0, pos)
    t.ontimer(mowe1,300)
t.tracer(False)

def func_up():
    global a, b
    a = 1
    b = 20
def func_down():
    global a, b
    a = 1
    b = -20
def func_right():
    global a, b
    a = 0
    b = 20

def func_left():
    global a, b
    a = 0
    b = -20

mowe1()

t.onkeypress(func_up, 'Up')
t.onkeypress(func_down, 'Down')
t.onkeypress(func_right, 'Right')
t.onkeypress(func_left, 'Left')
t.listen()




t.done()